var wl = require("../components/winLocation.js");
var myDialog = require("./cmodal.js");
var num = require("../components/number.js");
var stageNumber = 6;
var cardFeeRate = 0.00;
var applyNum = 0.00;
var maxApply = 0.00;
var isAllow = false;
var openid = '';

var merName, merId;
$(function() {
    wl.QueryString.Initial();
    initVale();
    $("#stageNumberBox>span").on("touchstart", selectStageNumber);
    $("#applyInstalmentNum").on("input", inputApplyInstalmentNum);
    inint();
    initAgreementCheck();
    $("#next-step").on("touchstart", function() {
        nextStepFunc();
    });
    setContractHref();
});

//  设置合同的连接
function setContractHref() {
    var theHref = "contract.html?openid=" + openid + "&cardFeeRate=" + cardFeeRate + "&merId=" + merId + "&merName=" + merName + "&lfq_cnt=" + stageNumber;
    if (applyNum != 0.00 && applyNum != "") {
        theHref += "&lfq_amt=" + applyNum;
    }
    $("#contract").attr("href", theHref);
}

//  初始化变量
function initVale() {
    cardFeeRate = parseFloat(wl.QueryString.GetValue("cardFeeRate"));
    merName = wl.QueryString.GetValue("merName");
    merId = wl.QueryString.GetValue("merId");
    openid = wl.QueryString.GetValue("openid");
    if (merName) {
        document.title =decodeURI(merName);
    }

}

// 初始化页面
function inint() {
    maxApply = 20000 / (1 + cardFeeRate / 100);
    $("#maxApplyNum").text(Math.floor(maxApply));
    $("#applyInstalmentNum").attr("placeholder", "目前只支持600-" + Math.floor(maxApply));
//    $("#merName").text(decodeURI(merName));
    setEachFee();
    if (wl.QueryString.GetValue("lfq_cnt")) {
        stageNumber = parseInt(wl.QueryString.GetValue("lfq_cnt"));
        var theselecter = 'span[data-number="' + stageNumber + '"]';
        var $parentNode = $(theselecter);
        setStageNumClass($parentNode);
    }
    if (wl.QueryString.GetValue("lfq_amt")) {
        $("#applyInstalmentNum").val(wl.QueryString.GetValue("lfq_amt"));
        inputApplyInstalmentNum();
    }
}

// 申请金额输入
function inputApplyInstalmentNum() {
    $applyInstalmentNum = $("#applyInstalmentNum");
    num.input2bit($applyInstalmentNum[0]);
    num.deleteNoNumber($applyInstalmentNum[0]);
    num.monreMax($applyInstalmentNum[0], maxApply.toFixed(2));
    applyNum = $applyInstalmentNum.val();
    if (applyNum == "") {
        applyNum = 0.00;
    } else {
        applyNum = parseFloat(applyNum);
    }

    computesEachPayNumber();
    allowApplyNum();
    isDisabledNext();
    setContractHref();
}


//选择分期数改变
function selectStageNumber() {
    var $parentNode = $(event.target.parentNode);
    if ($(event.target).data("number")) {
        $parentNode = $(event.target);
    }
    setStageNumClass($parentNode);
    stageNumber = parseInt($parentNode.attr("data-number"));
    computesEachPayNumber();
    isDisabledNext();
    setContractHref();
}

// 设置选择分期的样式
function setStageNumClass($parentNode) {
    $("#stageNumberBox>span").removeClass("checked");
    $parentNode.addClass("checked");
}

// 技术总金额，和每期金额
function computesEachPayNumber() {
    setEachFee();
    var contentMonet = parseFloat(applyNum * (parseFloat(cardFeeRate) / 100) + 0.00001 + applyNum).toFixed(2);

    var eachMoney = num.hold2bit(parseFloat(contentMonet) / stageNumber);
    var eachPrincipal = num.hold2bit(applyNum / stageNumber);
    var eachFeeNum = parseFloat(eachMoney) - parseFloat(eachPrincipal);
    $("#eachFee").next().text(Math.round(parseFloat(applyNum) * parseFloat(cardFeeRate)) / 100);
    $(".eachFeeNum").text(eachFeeNum.toFixed(2));
    $("#eachPrincipal").text(eachPrincipal);
    $("#eachMoney").text(eachMoney.toFixed(2));
    $("#countMoney").text(contentMonet);
}

function setEachFee() {
    var eachFee = cardFeeRate / stageNumber;
    $("#eachFee").text(eachFee.toFixed(2) + '%');
}

// 提示是否在允许的申请金额内
function allowApplyNum() {

    if (applyNum < 600 || applyNum > maxApply) {
        isAllow = false;
        $("label[for=applyInstalmentNum]").next().show();
    } else {
        isAllow = true;
        $("label[for=applyInstalmentNum]").next().hide();
    }

    return isAllow;
}

function initAgreementCheck() {
    var $agreementCheck = $("#agreement-check");
    $agreementCheck.on("change", agreementCheckChange);
    $("#checkButton").on("touchstart", agreementCheckParentFunc);
}

function agreementCheckParentFunc() {
    var $agreementCheck = $("#agreement-check");
    if ($agreementCheck.is(":checked")) {
        $agreementCheck.prop('checked', false);
    } else {
        $agreementCheck.prop('checked', true);
    }
    agreementCheckChange();
}

//已阅读协议选择改变
function agreementCheckChange() {
    var $agreementCheck = $("#agreement-check");
    if ($agreementCheck.is(":checked")) {
        $agreementCheck.parent().addClass("checked");
    } else {
        $agreementCheck.parent().removeClass("checked");
    }
    isDisabledNext();
}

// 是否允许下一步
function isDisabledNext(argument) {
    var agreementValue = $("#agreement-check").is(":checked");
    if (agreementValue && isAllow) {
        $("#next-step").attr("class", "btn btn_primary");
    } else {
        $("#next-step").attr("class", "btn btn_disabled btn_default");
    }
}

// 下一步 拼参数 发起请求
function nextStepFunc() {
    var agreementValue = $("#agreement-check").is(":checked");
    if (agreementValue && isAllow) {
        window.location.href = "applyNextStep.html?&lfq_amt=" + applyNum + "&lfq_cnt=" + stageNumber + "&openid=" + openid + "&merId=" + merId + "&cardFeeRate=" + cardFeeRate + "&merName=" + merName;
    }
}